package com.ad2campaign.engine.core.bootstrap;

import java.io.IOException;
import java.net.Socket;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

import com.ad2campaign.engine.utils.BootOperation;
import com.ad2campaign.engine.utils.PropertyLoader;

public class ShutDown {

	/**
	 * To stop engine
	 * @param args command line argument
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException 
	{
 		Properties engineProperties=PropertyLoader.settings();
 		int shutdownPort= Integer.parseInt( engineProperties.getProperty("engine.opsport", "9009"));
 		System.out.println("Connecting to shutdown socket  [127.0.0.1 : "+shutdownPort+"]");
 		Socket shutdownSocket = new Socket("127.0.0.1",shutdownPort);
 		System.out.println("Sending graceful shutdown signal...");
  		IOUtils.write(BootOperation.SHUTDOWN.getOperation()+"\n", shutdownSocket.getOutputStream());
  		
 		try	{shutdownSocket.getOutputStream().close();}	catch (Exception e)	{}
 		try	{shutdownSocket.close();}	catch (Exception e)	{}
 		System.out.println("Shutdown completed");

 		
	}

}
